package cn.usts.edu.lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {

    public ImageIconDemo() {
        JLabel label01 = new JLabel("图片icon");
        JButton button = new JButton();

        URL url = ImageIconDemo.class.getResource("csdn.png");// 获取图片位置
        ImageIcon imageIcon = new ImageIcon(url);// 实例化新的图片icon

        label01.setIcon(imageIcon);
        label01.setHorizontalAlignment(SwingConstants.CENTER);

        button.setIcon(imageIcon);
        button.setBounds(100,100,200,100);
        button.setToolTipText("图片按钮");// 鼠标悬停上面会提示的文字

        Container container = this.getContentPane();
        container.add(button);
        container.add(label01);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setBounds(100,100,600,600);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}
